package com.example.sms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.sms.entity.Course;
import com.example.sms.entity.Teacher;
import com.example.sms.entity.dto.LoginDTO;
import com.example.sms.entity.dto.RegisterDTO;
import com.example.sms.exception.R;
import com.example.sms.exception.SimpleException;
import com.example.sms.mapper.TeacherMapper;
import com.example.sms.response.ResponseModel;
import com.example.sms.service.CourseService;
import com.example.sms.service.StudentService;
import com.example.sms.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Tag( description = "教师管理" ,name = "教师管理" )
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseService courseService;

    @Operation(summary = "移除学生")
    @PostMapping("DeleteStudent")
    public Object delete(Integer teacherId , Integer studentId){
        //检查是否有该学生
        if (studentService.getById(studentId) == null)
            return R.fail("该学生不存在");
        //检查是否有该教师
        if (teacherService.getById(teacherId) == null)
            return R.fail("该教师不存在");
        //检查教师是否拥有该学生
        if (!courseService.checkTeacherStudent(teacherId, studentId)) {
            return R.fail("该教师未添加该学生");
        }
        //移除
        log.info("教师 ：" + teacherId +"移除学生：" + studentId);
        return R.ok(teacherService.delStudent(teacherId , studentId));
    }
    @Operation(summary = "更新教师信息")
    @PostMapping("UpdateTeacherInfo")
    public R update(@RequestBody Teacher teacher){
       Teacher teacher1 = teacherService.getOne(new QueryWrapper<Teacher>().lambda()
                .eq(Teacher::getUsername, teacher.getUsername())
                .eq(Teacher::getPassword, teacher.getPassword()));
        if (teacher1 == null){
            return R.fail("用户不存在");
        }else {
        log.info("更新教师信息 ：" + teacher.getUsername());
        teacher.setId(teacher1.getId());
        return R.ok(teacherService.updateById(teacher));
        }
    }
    @Operation(summary = "获取教师信息")
    @GetMapping("GetInfo")
    public Object info(Integer id){
        log.info("获取教师信息：" + id);
        return R.ok(teacherService.getById(id));
    }
    @Operation(summary = "添加课程")
    @PostMapping("AddCourse")
    public R addCourse(Integer teacherId,Integer courseId){
        log.info("教师：" + teacherId + "添加课程：" + courseId);
        //检查课程表是否已经存在
        Course course = courseService.getById(courseId);
        if (course == null){
            return R.fail("课程不存在");
        }
        //检查是否有该教师
        Teacher teacher = teacherMapper.selectById(teacherId);
        if (teacher == null){
            return R.fail("教师不存在");
        }
        //检查映射表有没有该记录
        if (courseService.checkTeacherCourse(teacherId, courseId))
            return R.fail("该教师已添加该课程");
        else
            return R.ok(teacherService.addCourse(teacherId, courseId));
    }
    @Operation(summary = "获取教师课程")
    @GetMapping("GetCourse")
    public Object course(Integer teacherId){
        log.info("获取教师课程：" + teacherId);
        return R.ok(teacherService.getTeacherCourses(teacherId));
    }

}
